package xredis

import (
	"context"
	"fmt"
	"time"
)

// RedisCounter Redis 计数器
type RedisCounter struct {
	client *Client
	key    string
}

func NewRedisCounter(client *Client, key string, keyFmtArgs ...interface{}) *RedisCounter {
	if len(keyFmtArgs) > 0 {
		key = fmt.Sprintf(key, keyFmtArgs...)
	}
	return &RedisCounter{
		client: client,
		key:    key,
	}
}

func (c *RedisCounter) Incr(ctx context.Context) int64 {
	return c.client.Incr(ctx, c.key).Val()
}

func (c *RedisCounter) Expire(ctx context.Context, duration time.Duration) error {
	return c.client.Expire(ctx, c.key, duration).Err()
}
